package com.sxl.main.executor;

import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorsTest {
    public static void main(String[] args) {
        int x = 1/0;
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(2, 2,
                30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5));
        long l = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
//            try {
                executorService.execute(() -> {
                    sayHi("submit");
                });
//            } catch (Exception e) {
//                System.out.println("sayHi Exception");
//                e.printStackTrace();
//            }
        }

//        System.out.println(System.currentTimeMillis()-l);
//        l = System.currentTimeMillis();
//        try {
//            final Object o = future.get();
//            System.out.println(0);
//        } catch (Exception e) {
//            System.out.println("future.get Exception");
//            e.printStackTrace();
//        }
//        System.out.println(System.currentTimeMillis()-l);
    }

    private static void sayHi(String name) throws RuntimeException {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String printStr = "【thread-name:" + Thread.currentThread().getName() + ",执行方式:" + name + "】";
        System.out.println(printStr);
        throw new RuntimeException(printStr + ",我异常啦!哈哈哈!");
    }
}
